package org.patterns.creational.builder;


import java.util.Objects;

public class ActorDirector {
    //逐步构建复杂产品对象
    public Actor construct(ActorBuilder builder) {
        Objects.requireNonNull(builder, "ActorBuilder 不能为 null");

        Actor actor;
        try {
            builder.buildType();
            builder.buildSex();
            builder.buildFace();
            builder.buildCostume();
            // 通过钩子方法来控制产品的构建过程
            if(!builder.isBareheaded()) {
                builder.buildHairstyle();
            }
            actor = builder.createActor();

            // 验证 actor 是否构建成功
            if (actor == null) {
                throw new IllegalStateException("构建的 Actor 不能为 null");
            }

        } catch (Exception ex) {
            ex.printStackTrace();
            throw ex;
        }

        return actor;
    }
}